package com.stxx.stiot2.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.stxx.framwork.domain.R;
import com.stxx.framwork.domain.model.Option;
import com.stxx.framwork.support.OptionInterface;
import com.stxx.framwork.support.OptionsService;
import com.stxx.stiot2.service.impl.GatewayServiceImpl;
import org.noear.solon.Solon;
import org.noear.solon.annotation.*;
import org.noear.solon.core.AppContext;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Mapping("/options")
public class OptionsController {
    @Inject
    AppContext context;

    @Get
    @Mapping("/{code}")
    public List<Option> optionList(@Path String code) {
        return getOptions(StrUtil.upperFirst(code));
    }

    @Get
    @Mapping("/arr/{codes}")
    public Map<String, List<Option>> getOptions2(@Path String[] codes) {
        Map<String, List<Option>> map = new HashMap<>();
        for (String code : codes) {
            List<Option> list = getOptions(StrUtil.upperFirst(code));
            map.put(code, list);
        }
        return map;
    }

    @Post
    @Mapping("/table")
    public Map<String, List<Option>> getOptions2(@Body List<Map<String,Object>> params) {
        Map<String, List<Option>> map = new HashMap<>();
        params.forEach(item->{
            String table = item.get("table").toString();
            try {
                Class<?> clazz =  Class.forName("com.stxx.stiot2.service."+StrUtil.upperFirst(StrUtil.toCamelCase(table))+"Service");
                if(OptionsService.class.isAssignableFrom(clazz)){
                    OptionsService optionsService = (OptionsService)Solon.context().getBean(clazz);
                    if(optionsService==null){
                        return;
                    }
                    try {
                        map.put(table,optionsService.queryOptions(item.get("ids").toString().split(",")));
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        });
        return map;
    }

    @Get
    @Mapping("/table/{table}")
    public List<Option> tableSearch(@Path String table,@Param(required = false) String id, @Param(required = false) String w,@Param(required = false) String pid) {
        try {
            Class<?> clazz = Class.forName("com.stxx.stiot2.service."+ StrUtil.upperFirst(StrUtil.toCamelCase(table))+"Service");
            if(OptionsService.class.isAssignableFrom(clazz)){
                OptionsService optionsService = (OptionsService)Solon.context().getBean(clazz);
                if(optionsService==null){
                    return null;
                }
                return optionsService.queryOptions(id,w,pid);
            }
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }

        return null;
    }


    private List<Option> getOptions(String code) {
        try {
            List<Option> list = new ArrayList<>();
            Class<Enum> clazz = (Class<Enum>)Class.forName("com.stxx.framwork.constants."+code+"Type");
            Enum[] enumConstants = clazz.getEnumConstants();
            for (Enum enumItem : enumConstants) {
                OptionInterface optionInterface = (OptionInterface)enumItem;
                Option hashMap = new Option();
                hashMap.setValue(optionInterface.getValue());
                hashMap.setLabel(optionInterface.getLabel());
                list.add(hashMap);
            }
            return list;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
